/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package HJT3;

/**
 *
 * @author julio
 */
import java.io.*;
import java.util.Random;

public class FileGenerator {
    
    Random rand = new Random();
    
    public void createOriginalFile(){
        FileGenerator gen = new FileGenerator();
        int x = 2000;
        try{
            FileWriter fstream = new FileWriter("file.txt");
            BufferedWriter out = new BufferedWriter(fstream);
            for(int c = 0; c<x; c++){
                out.write(String.valueOf(gen.randomGenerator()));
                out.write(" ");
            }
            out.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    public void createSortedFiles(String title, int[] list){
        FileGenerator gen = new FileGenerator();
        int x = list.length;
        try{
            FileWriter fstream = new FileWriter(title+".txt");
            BufferedWriter out = new BufferedWriter(fstream);
            for(int c = 0; c<x; c++){
                out.write(String.valueOf(list[c]));
                out.write(" ");
            }
            out.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    public int randomGenerator(){

        int i;
        // Random integers that range from from 0 to n
        int n = 50;
        i = rand.nextInt(n+1);

        return i;
    }
    
    public int[] getArrayListFile(int[] list){
        BufferedReader reader = null;
        String line = null;
        int c = 0;
        try {
            reader = new BufferedReader(new FileReader("file.txt"));
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split("\\s");
                   for (String part : parts) {
                        Integer i = Integer.valueOf(part);
                        list[c] = i;
                        c++;
                    }
            }
        } catch (IOException e) {
        }
        return list;
    }
}

